<template>
  <div class="mine-management">
    <!-- 操作按钮和搜索区域 -->
    <div class="operation-area">
      <div class="left">
        <el-button type="primary" @click="handleAdd">新增矿山</el-button>
      </div>
      <div class="right">
        <el-input
          v-model="searchForm.keyword"
          placeholder="请输入关键字搜索"
          clearable
          @input="handleSearch"
          @clear="handleSearch"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="mineList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column prop="name" label="矿山名称" />
        <el-table-column prop="address" label="矿山地址" />
        <el-table-column prop="manager" label="矿山管理员" />
        <el-table-column prop="contact" label="联系方式" />
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination-area">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增矿山' : '编辑矿山'"
      width="500px"
    >
      <el-form
        ref="mineFormRef"
        :model="mineForm"
        :rules="mineFormRules"
        label-width="100px"
      >
        <el-form-item label="矿山名称" prop="name">
          <el-input v-model="mineForm.name" placeholder="请输入矿山名称" />
        </el-form-item>
        <el-form-item label="矿山地址" prop="address">
          <el-input v-model="mineForm.address" placeholder="请输入矿山地址" />
        </el-form-item>
        <el-form-item label="矿山管理员" prop="manager">
          <el-input v-model="mineForm.manager" placeholder="请输入矿山管理员" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="mineForm.contact" placeholder="请输入联系方式" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import { Search } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  keyword: ''
})

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 表格数据
const mineList = ref([
  {
    name: '示例矿山1',
    address: '北京市海淀区',
    manager: '张三',
    contact: '13800138000'
  },
  {
    name: '示例矿山2',
    address: '上海市浦东新区',
    manager: '李四',
    contact: '13900139000'
  },
  {
    name: '示例矿山3',
    address: '广州市天河区',
    manager: '王五',
    contact: '13700137000'
  },
  {
    name: '示例矿山4',
    address: '深圳市南山区',
    manager: '赵六',
    contact: '13600136000'
  },
  {
    name: '示例矿山5',
    address: '成都市高新区',
    manager: '孙七',
    contact: '13500135000'
  },
  {
    name: '示例矿山6',
    address: '武汉市洪山区',
    manager: '周八',
    contact: '13400134000'
  },
  {
    name: '示例矿山7',
    address: '杭州市西湖区',
    manager: '吴九',
    contact: '13300133000'
  },
  {
    name: '示例矿山8',
    address: '南京市玄武区',
    manager: '郑十',
    contact: '13200132000'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add') // 'add' 或 'edit'
const mineFormRef = ref(null)
const mineForm = reactive({
  name: '',
  address: '',
  manager: '',
  contact: ''
})

// 表单验证规则
const mineFormRules = {
  name: [{ required: true, message: '请输入矿山名称', trigger: 'blur' }],
  address: [{ required: true, message: '请输入矿山地址', trigger: 'blur' }],
  manager: [{ required: true, message: '请输入矿山管理员', trigger: 'blur' }],
  contact: [
    { required: true, message: '请输入联系方式', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ]
}

// 搜索方法
const handleSearch = () => {
  console.log('搜索关键字：', searchForm.keyword)
  // TODO: 调用接口进行搜索
  currentPage.value = 1
}

// 新增矿山
const handleAdd = () => {
  dialogType.value = 'add'
  Object.keys(mineForm).forEach(key => mineForm[key] = '')
  dialogVisible.value = true
}

// 编辑矿山
const handleEdit = (row) => {
  dialogType.value = 'edit'
  Object.keys(mineForm).forEach(key => mineForm[key] = row[key])
  dialogVisible.value = true
}

// 删除矿山
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除矿山"${row.name}"吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// 提交表单
const handleSubmit = async () => {
  if (!mineFormRef.value) return
  await mineFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 调用新增或编辑接口
      console.log('表单数据：', mineForm)
      ElMessage.success(dialogType.value === 'add' ? '新增成功' : '编辑成功')
      dialogVisible.value = false
      handleSearch()
    }
  })
}

// 分页方法
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.mine-management {
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.operation-area {
  padding: 0 10px;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.operation-area .right {
  width: 250px;
}

.table-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #fff;
  border-radius: 4px;
  padding: 0 10px;
  min-height: 0; /* 重要：确保flex子项可以正确收缩 */
}

.table-area :deep(.el-table) {
  flex: 1;
}

.table-area :deep(.el-table__inner-wrapper) {
  height: 100%;
}

.table-area :deep(.el-table__body-wrapper) {
  height: calc(100% - 40px) !important;
}

.pagination-area {
  padding: 10px;
  background-color: #fff;
  border-radius: 4px;
  margin-top: 8px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 添加一些测试数据来验证滚动 */
:deep(.el-table__body-wrapper) {
  overflow-y: auto !important;
}
</style>
